<?php
if(!defined('BASE_DIR'))die('Cannot access the file !');
/**
 * -+-----------------------------------------
 * |PHP Framework
 * |Copyright (c) 2010 jinke.[iblue.cc]
 * |E-mail: mejinke@gmail.com
 * |Date: 2010-03-10
 * -+-----------------------------------------
 *
 * 系统分页类
 * 控制器中调用方式：
 * $page=Functions::import('$.class.page'); //加载page类
 * $page->init(20,46); //初始化 20为每页显示数 46 总记录数
 * echo $page->show(); //输出
 * @author jinke
 */
class Page extends Base
{
	//每页显示数
	public $page_size;

	//总页数
	public $page_sum;

	//记录总数
	public $data_sum;

	//当前页码
	private $page = 1;

	public $ajax = false;
	public $arg = 'p';

	public $top_page = '上一页';
	public $next_page = '下一页';
	public $first_page = '首 页';
	public $last_page = '最后一页';
	public $diyArray = null;
	//指定的URL
	private $url='';

	public $args;
	private $args_index=null;
	private $style = '';

	/**
	 * 构造函数，设置必要信息
	 * @param unknown_type $array
	 */
	public function __construct()
	{
		$this->args =$this->getRouter()->getArgs();

		Debug::initClass('Page.class.php');
	}

	/**
	 * 设置必要的信息
	 * @param Int $size  每页显示数
	 * @param Int $datasize 数据总数
	 * @param String $style 样式名称
	 * @param	string	$url	开始位置的URL
	 */
	public function init($size,$datasize,$style='',Array $args=array(),$url='')
	{
		Functions::GET($this->arg) ? $page = Functions::GET($this->arg) : $page = 1;
		$this->page = $page;
		$this->page_size = $size;
		$this->data_sum = $datasize;
		$this->style = $style;
		$this->url	=	$url;

		//重置参数
		$this->args =$args;

		//获取总页数
		$this->page_sum = ceil($this->data_sum/$this->page_size);
		
		$this->getDiyResultByArray();
	}


	/**
	 * 重置参数
	 * @param	array	 $array
	 */
	public function setArgs(Array $array)
	{
		$this->args=$array;
	}


	/**
	 * 获取下一页
	 */
	public function getNext()
	{
		if ($this->page < $this->page_sum)
			return $this->get_link($this->page+1,$this->next_page);
		else
			return $this->span($this->next_page);
	}


	/**
	 * 获取上一页
	 */
	private function getTop()
	{
		if ($this->page > 1)
			return $this->get_link($this->page-1,$this->top_page);
		else
			return $this->span($this->top_page);
	}


	/**
	 *	获取第一页
	 */
	private function getFirst()
	{
		if ($this->page == 1)
			return $this->span($this->first_page);
		else
			return $this->get_link(1,$this->first_page);
	}


	/**
	 *	获取最后一页
	 */
	private  function getLast()
	{
		if ($this->page == $this->page_sum || $this->page_sum==0)
			return $this->span($this->last_page);
		else
			return $this->get_link($this->page_sum,$this->last_page);
	}


	/**
	 * 显示分页
	 */
	public function show()
	{
		return '共有'.$this->data_sum.'条记录 '.$this->getFirst().$this->getTop().$this->getNext().$this->getLast();
	}


	/**
	 *	显示分页：下拉列表
	 * @access public
	 */
	public function select($left='',$right='')
	{
		$str = '<select onchange="window.location.href=this.value">';
		for ($i = 1; $i <= $this->page_sum; $i++)
		{
			if ($this->page == $i)
				$str.='<option value="'.$this->getUrl($i).'" selected>'.$left.$i.$right.'</option>';
			else
				$str.='<option value="'.$this->getUrl($i).'">'.$left.$i.$right.'</option>';
		}
		$str.='</select>';
		return $str;
	}

    /**
	 * 类似百度翻页
	 * @access public
	 */
	public function nowbar($style='',$nowindex_style='')
	{
		$plus=ceil(6/2);
		if(6-$plus+$this->page>$this->page_sum)$plus=(6-$this->page_sum+$this->page);
		$begin=$this->page-$plus+1;
		$begin=($begin>=1)?$begin:1;
		$return='';
		$argp=$this->args;
		for($i=$begin;$i<$begin+6;$i++)
		{
			if($i<=$this->page_sum)
			{
				if($i!=$this->page)
					$return.=$this->get_link($i,$i);
				else
					$return.=' <span class="'.$nowindex_style.'">'.$i.'</span> ';
			}
			else
				break;
		   $return.="\n";
		  }

		  unset($begin);
		 // return  '共有'.$this->data_sum.'条记录 '.$this->getFirst().$this->getTop().'&nbsp;'.$return.'&nbsp;'.$this->getNext().$this->getLast();
		 $str = $this->getTop().'&nbsp;'.$return.'&nbsp;'.$this->getNext();
		  isset($argp['p'])?$argp['p']++:'';
		 $this->args=$argp;
		 return $str;
	 }
	 
	 /**
	  * 获取分页所必要的资料，用户可以根据该资料自定议的分页样式
	  * @access private
	  */
	 private function getDiyResultByArray(){
	 	$plus=ceil(6/2);
		if(6-$plus+$this->page>$this->page_sum)$plus=(6-$this->page_sum+$this->page);
		$begin=$this->page-$plus+1;
		$begin=($begin>=1)?$begin:1;
		$return=array();
		$return['html']['first'] = $this->_getUrl(1,1);
		$return['data']['first'] = 1;
		$return['html']['last'] = $this->_getUrl($this->page_sum,$this->page_sum);
		$return['data']['last'] = $this->page_sum;
		$argp=$this->args;
		$return['html']['nonce'] = $this->page;
		$return['data']['nonce'] = $this->page;
		//上一页
		$ps = $this->page==1?1:$this->page-1;
		
		$return['html']['previous'] = $this->_getUrl($ps,$ps);
		$return['data']['previous'] = $ps;
		//下一页
		$ps = $this->page==$this->page_sum?$this->page_sum:$this->page+1;
		$return['html']['next'] = $this->_getUrl($ps,$ps);
		$return['data']['next'] = $ps;
		$return['url'] = $this->url;
		//总页
		$return['data_count'] = $this->data_sum;
		for($i=$begin;$i<$begin+6;$i++)
		{
			if($i<=$this->page_sum)
			{
				$return['html']['item'][]=array('html'=>$this->_getUrl($i,$i),'number'=>$i);
				$return['data']['item'][]=$i;
			}
			else
				break;
		  }
		  //生成额外的参数连接
		  $string='';
		  $values = array_values($argp);
		  $keys = array_keys($argp);
		  for ($i=0;$i<count($argp);$i++){
		  	if ($keys[$i]!=$this->arg)
		  		$string.='/'.$keys[$i].'='.$values[$i];
		  }
		  $return['args_string'] = $string;
		  $return['args'] = $argp;
		  $this->diyArray = $return; 
		  $this->getView()->add('_page',$return);
		  //print_r($return);
	 }


	 /**
	   * 获取链接地址
	   */
	private function get_link($url,$text)
	{
	 	if (!empty($this->style))
	  		return ' <a  class="'.$this->style.'" '.$this->getUrl($url).'>'.$text.'</a> ';
	  	else
	  		return ' <a   '.$this->getUrl($url).'>'.$text.'</a> ';
	 }


 	private function span($text)
	{
 		if (!empty($this->style))
	  		 return  ' <span class="'.$this->style.'">'.$text.'</span> ';
	  	else
	  		return  ' <span>'.$text.'</span>';
	 }

	 /**
	  * 自动生成连接地址
	  * @param int $page 连接页码
	  */
	private function getUrl($page)
	{
		$this->args['p'] = $page;
		if (!$this->ajax)
	 		return 'href="'.url(array(),$this->args,'',$this->url).'"';
	 	else
	 		return 'href="javascript:void(0);" onclick="doAjaxPage('.$page.')"';
	 }
	 
	 private function _getUrl($page){
	 	$this->args['p'] = $page;
		if (!$this->ajax)
	 		return url(array(),$this->args,'',$this->url);
	 }

}
?>
